package com.kdzy.lshy.socity_server.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kdzy.lshy.socity_server.common.DateUtil;
import com.kdzy.lshy.socity_server.common.ExceptionDetailMessage;
import com.kdzy.lshy.socity_server.common.HttpToSMS;
import com.kdzy.lshy.socity_server.common.ResultMap;
import com.kdzy.lshy.socity_server.repository.BaseDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 推送消息service
 * @author cly
 * @since 2018-06-21
 */
@Service
public class SendMessageService extends BaseService {

    private static final Logger logger = LoggerFactory.getLogger(SendMessageService.class);

    @Resource
    private BaseDao baseDao;

    public Object sendMessage(String param) {
        LinkedHashMap<Object, Object> map = null;
        try {
            if (StringUtils.isNoneBlank(StringUtils.trim(param))){
                // 1.解析param
                JSONObject jsonObject = JSON.parseObject(param);
                String ID = (String)jsonObject.get("ID"); // 主键 bespeak（预约表中包含业务状态）
                Integer bizzType = jsonObject.getInteger("bizz_type"); // 0 已提交资料 1 预约2 本社区待审核 3 本社区通过（4失败）5第三方已提交 6第三方待审核 7第三方成功（8第三方失败） 9 其他

                Long stamp = (Long) jsonObject.get("stamp"); // 时间戳
                String createTime = DateUtil.format(new Date(stamp), "yyyy-MM-dd HH:mm:dd"); // 创建时间yyyy-MM-dd HH:mm:ss
                String updateTime = DateUtil.format(new Date(stamp), "yyyy-MM-dd HH:mm:dd"); // 创建时间yyyy-MM-dd HH:mm:ss
                Integer isUsed = jsonObject.getInteger("is_used"); // 是否失效 0:有效 1:无效
                Integer version = jsonObject.getInteger("version"); // 数据版本 0:测试 1:生产
                Integer visitPlat = jsonObject.getInteger("visit_plat"); // 访问平台 0网页 1一体机 2小程序 3其他
                Integer visitType = jsonObject.getInteger("visit_type"); // 访问类型 0测试 1生产

                // 参数非空判断
                if (bizzType == null) {
                    logger.info("参数bizz_type为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }
                if (stamp == null) {
                    logger.info("参数stamp为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }
                if (version == null) {
                    logger.info("参数version为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }
                if (isUsed == null) {
                    logger.info("参数is_used为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }
                if (visitPlat == null) {
                    logger.info("参数visit_plat为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }
                if (visitType == null) {
                    logger.info("参数visit_type为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }

                // 动态参数集合
                List<Object> tags = new ArrayList<Object>();

                // 编写sql
                StringBuilder sql = new StringBuilder("SELECT\n" +
                        "\tdw.dweller_name dwellerName,\n" +
                        "\tddb.dweller_id dwellerId,\n" +
                        "\tdw.telphone dwellerPhone,\n" +
                        "\tdep.dep_name depName,\n" +
                        "\tddb.dep_id depId,\n" +
                        "\toff.ID offId,\n" +
                        "\tbz.bizz_name bizzName,\n" +
                        "\tddb.bizz_id bizzId,\n" +
                        "\tddb.bizz_type bizzType,\n" +
                        "\tddb.bizz_fall_res bizzFallRes,\n" +
                        "\tddb.bizz_succ_note bizzSuccNote\n" +
                        "FROM\n" +
                        "\tbespeak ddb,\n" +
                        "\tdweller dw,\n" +
                        "\tbizz bz,\n" +
                        "\tdepartments dep,\n" +
                        "\tofficer off\n" +
                        "WHERE\n" +
                        "\tddb.dweller_id = dw.ID\n" +
                        "AND ddb.bizz_id = bz.ID\n" +
                        "AND ddb.officer_id = off.ID\n" +
                        "AND ddb.dep_id = dep.ID");
                if (StringUtils.isNoneBlank(StringUtils.trim(ID))){
                    sql.append(" AND ddb.ID = ?");
                    tags.add(ID);
                }
                logger.info("sql = " + sql.toString());

                // 2.调用dao查询居民电话
                List<Map<String, Object>> mapList = baseDao.queryForList(sql.toString(), tags.toArray());

                if (mapList == null || mapList.size() == 0) {
                    logger.info("该居民未注册手机号");
                    map = ResultMap.creatReault("12", "失败", "该居民未注册手机号");
                    logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }

                if (mapList != null && mapList.size() > 0){
                    if (mapList.get(0) != null) {
                        // 3.根据业务修改的状态给居民发送短信消息
                        //  赋值参数
                        String dwellerName = (String) mapList.get(0).get("dwellerName"); // 居民姓名
                        String dwellerId = (String) mapList.get(0).get("dwellerId"); // 居民姓名
                        String dwellerPhone = (String) mapList.get(0).get("dwellerPhone"); // 居民电话
                        String depName = (String) mapList.get(0).get("depName"); // 部门名称
                        String depId = (String) mapList.get(0).get("depId"); // 部门Id
                        String bizzName = (String) mapList.get(0).get("bizzName"); // 业务名称
                        String bizzId = (String) mapList.get(0).get("bizzId"); // 业务名称
                        String offId = (String) mapList.get(0).get("offId"); // 工作人员id
                        String bizzFallRes = (String) mapList.get(0).get("bizzFallRes"); // 业务失败原因
                        String bizzSuccNote = (String) mapList.get(0).get("bizzSuccNote"); // 业务成功补充说明
                        String bizzTypeInfo = ""; // 业务状态说明
                        switch (bizzType) {
                            case 0:
                                bizzTypeInfo = "已提交资料";
                                break;
                            case 1:
                                bizzTypeInfo = "预约";
                                break;
                            case 2:
                                bizzTypeInfo = "本社区待审核";
                                break;
                            case 3:
                                bizzTypeInfo = "本社区审核通过";
                                break;
                            case 4:
                                bizzTypeInfo = "本社区审核未通过";
                                break;
                            case 5:
                                bizzTypeInfo = "相关资料已提交第三方";
                                break;
                            case 6:
                                bizzTypeInfo = "第三方待审核";
                                break;
                            case 7:
                                bizzTypeInfo = "第三方已审核通过";
                                break;
                            case 8:
                                bizzTypeInfo = "第三方审核未通过";
                                break;
                                default:
                                    bizzTypeInfo = "其它";
                                    break;
                        }

                        // 组织语言
                        StringBuilder sendMsgContent = new StringBuilder(dwellerName + "您好，您在" + depName + "窗口办理的" + bizzName + "业务进度更新：" + bizzTypeInfo + "。");
                        if (bizzType == 3 || bizzType == 7) {
                            if (StringUtils.isNoneBlank(StringUtils.trim(bizzSuccNote))) {
                                sendMsgContent.append("备注：" + bizzSuccNote);
                                bizzTypeInfo = bizzTypeInfo + "，备注：" + bizzSuccNote;
                            }
                        }
                        if (bizzType == 4 || bizzType == 8) {
                            if (StringUtils.isNoneBlank(StringUtils.trim(bizzFallRes))) {
                                sendMsgContent.append("备注：" + bizzFallRes);
                                bizzTypeInfo = bizzTypeInfo + "，备注：" + bizzFallRes;
                            }
                        }

                        // 封装发送短信相关参数
                        Map<String, Object> msgMap = new HashMap<>();
                        msgMap.put("dweTelephone", dwellerPhone);
                        msgMap.put("dweName", dwellerName);
                        msgMap.put("depName", depName);
                        msgMap.put("bizzName", bizzName);
                        msgMap.put("res", bizzTypeInfo);
                        String msgParam = JSON.toJSONString(msgMap);

                        // 调用第三方发送短信接口发送信息
                        String msgResponse = (String) HttpToSMS.sendSMS(msgParam);
                        if (msgResponse == null) {
                            logger.info("发送失败");
                            map = ResultMap.creatReault("11", "失败", "短信发送失败");
                            logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                        }
                        if (msgResponse != null) {
                            JSONObject msgJsonObject = JSON.parseObject(msgResponse);
                            if (StringUtils.equals(msgJsonObject.getString("status"), "-1")) { // 发送失败
                                logger.info("发送失败");
                                map = ResultMap.creatReault("11", "失败", "短信发送失败");
                                logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                            }

                            if (StringUtils.equals(msgJsonObject.getString("status"), "99")) { // 发送失败
                                logger.info("发送失败");
                                map = ResultMap.creatReault("11", "失败", "短信发送失败");
                                logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                            }

                            if (StringUtils.equals(msgJsonObject.getString("status"), "0")) { // 发送成功
                                logger.info("发送成功");
                                map = ResultMap.creatReault("1", "成功", "短信发送成功");

                                // 将发送的信息存入数据库
                                // 1.更新dweller_message表数据
                                String msgId = UUID.randomUUID().toString().replace("-", ""); // 消息唯一标识
                                Integer msgType = 0; // 0:短信 1:微信 2:站内
                                String sqlInsert = "INSERT INTO \n" +
                                        "\tmessage(\n" +
                                        "\t\tID,\n" +
                                        "\t\tdep_id,\n" +
                                        "\t\tbizz_id,\n" +
                                        "\t\tdweller_id,\n" +
                                        "\t\tofficer_id,\n" +
                                        "\t\tcontent,\n" +
                                        "\t\tTYPE,\n" +
                                        "\t\tcreate_time,\n" +
                                        "\t\tupdate_time,\n" +
                                        "\t\tstamp,\n" +
                                        "\t\tversion,\n" +
                                        "\t\tvisit_plat,\n" +
                                        "\t\tvisit_type,\n" +
                                        "\t\tis_used\n" +
                                        "\t)\n" +
                                        "VALUES\n" +
                                        "\t\t(\n" +
                                        "\t\t'" + msgId + "',\n" +
                                        "\t\t'" + depId + "',\n" +
                                        "\t\t'" + bizzId + "',\n" +
                                        "\t\t'" + dwellerId + "',\n" +
                                        "\t\t'" + offId + "',\n" +
                                        "\t\t'" + sendMsgContent + "',\n" +
                                        "\t\t" + msgType + ",\n" +
                                        "\t\t'" + createTime + "',\n" +
                                        "\t\t'" + updateTime + "',\n" +
                                        "\t\t'" + stamp + "',\n" +
                                        "\t\t" + version + ",\n" +
                                        "\t\t" + visitPlat + ",\n" +
                                        "\t\t" + visitType + ",\n" +
                                        "\t\t" + isUsed + "\n" +
                                        "\t)";
                                // 调用数据库
                                int update = baseDao.update(sqlInsert);
                                if (update <= 0) {
                                    logger.info("插入message消息表失败");
                                    map = ResultMap.creatReault("77", "失败", "数据库操作失败");
                                    logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                                    return map;
                                }

                                // 2.更新message表数据
                                String dwellerMsgId = UUID.randomUUID().toString().replace("-", ""); // 消息唯一标识

                                // 封装sql
                                sqlInsert = "INSERT INTO \n" +
                                        "\tdweller_message(\n" +
                                        "\t\tID, \n" +
                                        "\t\tmessage_id,\n" +
                                        "\t\tdweller_id,\n" +
                                        "\t\tTYPE,\n" +
                                        "\t\tread_type,\n" +
                                        "\t\tcreate_time,\n" +
                                        "\t\tupdate_time,\n" +
                                        "\t\tstamp,\n" +
                                        "\t\tversion,\n" +
                                        "\t\tvisit_plat,\n" +
                                        "\t\tvisit_type,\n" +
                                        "\t\tis_used\n" +
                                        "\t)\n" +
                                        "VALUES\n" +
                                        "\t\t(\n" +
                                        "\t\t'" + dwellerMsgId + "',\n" +
                                        "\t\t'" + msgId + "',\n" +
                                        "\t\t'" + dwellerId + "',\n" +
                                        "\t\t" + msgType + ",\n" +
                                        "\t\t" + 1 + ",\n" +
                                        "\t\t'" + createTime + "',\n" +
                                        "\t\t'" + updateTime + "',\n" +
                                        "\t\t'" + stamp + "',\n" +
                                        "\t\t" + version + ",\n" +
                                        "\t\t" + visitPlat + ",\n" +
                                        "\t\t" + visitType + ",\n" +
                                        "\t\t" + isUsed + "\n" +
                                        "\t)";
                                // 调用数据源
                                int resUpdate = baseDao.update(sqlInsert);
                                if (resUpdate <= 0) {
                                    logger.info("插入dweller_message居民消息关联表失败");
                                    map = ResultMap.creatReault("77", "失败", "数据库操作失败");
                                    logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
                                    return map;
                                }

                                logger.info("保存数据成功");
                                map = ResultMap.creatReault("0", "成功", "保存数据成功");
                            }
                        }
                    }
                }
            } else {
                logger.info("参数为空");
                map = ResultMap.creatReault("88", "失败", "参数为空");
            }
        } catch (ClassCastException e) {
            logger.info("后台处理异常，异常信息1：" + ExceptionDetailMessage.getExceptionDetail(e));
            map = ResultMap.creatReault("98", "失败", "参数类型错误");
            logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
            return map;
        } catch (Exception e) {
            logger.info("后台处理异常，异常信息1：" + ExceptionDetailMessage.getExceptionDetail(e));
            map = ResultMap.creatReault("99", "失败", "后台处理异常");
            return map;
        }

        // 4.返回发送成功或者失败
        logger.info("出去《推送消息 - 短信》list()方法；出参 = " + JSON.toJSONString(map));
        return map;
    }

    public Object sendResidencePermit(String param) {
        LinkedHashMap<Object, Object> map = null;
        try {
            if (StringUtils.isNoneBlank(StringUtils.trim(param))){
                // 1.解析param
                JSONObject jsonObject = JSON.parseObject(param);
                String ID = (String)jsonObject.get("ID"); // 主键 socity_quick（社区直通车表中包含业务状态）
                Integer resType = jsonObject.getInteger("res_type"); // 1:提交材料 2:资料有误 3:资料正确  4:发送上门短信通知  5:上门审核后失败（不在家） 6:上门审核后成功 7:已发放 8:上门审核后失败（在家）

                Long stamp = (Long) jsonObject.get("stamp"); // 时间戳
                String createTime = DateUtil.format(new Date(stamp), "yyyy-MM-dd HH:mm:dd"); // 创建时间yyyy-MM-dd HH:mm:ss
                String updateTime = DateUtil.format(new Date(stamp), "yyyy-MM-dd HH:mm:dd"); // 创建时间yyyy-MM-dd HH:mm:ss
                Integer isUsed = jsonObject.getInteger("is_used"); // 是否失效 0:有效 1:无效
                Integer version = jsonObject.getInteger("version"); // 数据版本 0:测试 1:生产
                Integer visitPlat = jsonObject.getInteger("visit_plat"); // 访问平台 0网页 1一体机 2小程序 3其他
                Integer visitType = jsonObject.getInteger("visit_type"); // 访问类型 0测试 1生产

                // 参数非空判断
                if (resType == null) {
                    logger.info("参数res_type空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }
                if (stamp == null) {
                    logger.info("参数stamp为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }
                if (version == null) {
                    logger.info("参数version为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }
                if (isUsed == null) {
                    logger.info("参数is_used为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }
                if (visitPlat == null) {
                    logger.info("参数visit_plat为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }
                if (visitType == null) {
                    logger.info("参数visit_type为空");
                    map = ResultMap.creatReault("88", "失败", "参数为空");
                    logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }

                String toHomeDate = null;
                if (resType == 4) {
                    logger.info("需要上门审核");
                    toHomeDate = jsonObject.getString("toHomeDate");

                    if (!StringUtils.isNoneBlank(StringUtils.trim(toHomeDate))) {
                        logger.info("参数toHomeDate为空");
                        map = ResultMap.creatReault("88", "失败", "参数为空");
                        logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                        return map;
                    }
                }

                // 动态参数集合
                List<Object> tags = new ArrayList<Object>();

                // 编写sql
                StringBuilder sql = new StringBuilder("SELECT\n" +
                        "\tdw.dweller_name dwellerName,\n" +
                        "\tsq.dweller_id dwellerId,\n" +
                        "\tdw.telphone dwellerPhone,\n" +
                        "\tdep.dep_name depName,\n" +
                        "\tdb.dep_id depId,\n" +
                        "\toff.ID offId,\n" +
                        "\tbz.bizz_name bizzName,\n" +
                        "\tsq.bizz_id bizzId,\n" +
                        "\tsq.res_type resType,\n" +
                        "\tsq.res_note resNote\n" +
                        "FROM\n" +
                        "\tsocity_quick sq,\n" +
                        "\tdweller dw,\n" +
                        "\tbizz bz,\n" +
                        "\tdepartments dep,\n" +
                        "\tdepartment_bizz db,\n" +
                        "\tofficer off\n" +
                        "WHERE\n" +
                        "\tsq.dweller_id = dw.ID\n" +
                        "AND sq.bizz_id = bz.ID\n" +
                        "AND\n" +
                        "IF (\n" +
                        "\t(\n" +
                        "\t\tsq.bizz_id IN (\n" +
                        "\t\t\tSELECT\n" +
                        "\t\t\t\tbizz.bizz_parent_id\n" +
                        "\t\t\tFROM\n" +
                        "\t\t\t\tbizz\n" +
                        "\t\t\tWHERE\n" +
                        "\t\t\t\tbizz.bizz_parent_id IS NOT NULL\n" +
                        "\t\t\tAND bizz.bizz_parent_id != ''\n" +
                        "\t\t)\n" +
                        "\t),\n" +
                        "\t(bz.ID = db.bizz_id),\n" +
                        "\t(bz.bizz_parent_id = db.bizz_id)\n" +
                        ")\n" +
                        "AND db.dep_id = dep.ID\n" +
                        "AND sq.officer_id = off.ID");
                if (StringUtils.isNoneBlank(StringUtils.trim(ID))){
                    sql.append(" AND sq.ID = ?");
                    tags.add(ID);
                }
                logger.info("sql = " + sql.toString());

                // 2.调用dao查询居民电话
                List<Map<String, Object>> mapList = baseDao.queryForList(sql.toString(), tags.toArray());

                if (mapList == null || mapList.size() == 0) {
                    logger.info("该居民未注册手机号");
                    map = ResultMap.creatReault("12", "失败", "该居民未注册手机号");
                    logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                    return map;
                }

                if (mapList != null && mapList.size() > 0){
                    if (mapList.get(0) != null) {
                        // 3.根据业务修改的状态给居民发送短信消息
                        //  赋值参数
                        String dwellerName = (String) mapList.get(0).get("dwellerName"); // 居民姓名
                        String dwellerId = (String) mapList.get(0).get("dwellerId"); // 居民姓名
                        String dwellerPhone = (String) mapList.get(0).get("dwellerPhone"); // 居民电话
                        String depName = (String) mapList.get(0).get("depName"); // 部门名称
                        String depId = (String) mapList.get(0).get("depId"); // 部门Id
                        String bizzName = (String) mapList.get(0).get("bizzName"); // 业务名称
                        String bizzId = (String) mapList.get(0).get("bizzId"); // 业务名称
                        String offId = (String) mapList.get(0).get("offId"); // 工作人员id
                        String resNote = (String) mapList.get(0).get("resNote"); // 业务失败原因
                        String bizzTypeInfo = ""; // 业务状态说明
                        switch (resType) {
                            case 1:
                                bizzTypeInfo = "已提交资料";
                                break;
                            case 2:
                                bizzTypeInfo = "您提交的资料有误，请重新上传";
                                break;
                            case 3:
                                bizzTypeInfo = "您提交的资料正确";
                                break;
                            case 4:
                                bizzTypeInfo = toHomeDate;
                                break;
                            case 5:
                                bizzTypeInfo = "上门审核后失败（不在家）";
                                break;
                            case 6:
                                bizzTypeInfo = "上门审核后成功";
                                break;
                            case 7:
                                bizzTypeInfo = "已发放";
                                break;
                            case 8:
                                bizzTypeInfo = "上门审核后失败";
                                break;
                            default:
                                bizzTypeInfo = "其它";
                                break;
                        }

                        // 组织语言
                        StringBuilder sendMsgContent = null;

                        if (resType == 1 || resType == 7) { // 不让发短信
                            logger.info("该状态下不允许发送短信");
                            map = ResultMap.creatReault("12", "失败", "该状态下不允许发送短信");
                            logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                            return map;
                        }

                        if (resType == 4) { // 上门审核通知
                            sendMsgContent = new StringBuilder(dwellerName + "您好，您在" + depName + "窗口办理的" + bizzName + "业务审核通知：社区人员将于" + bizzTypeInfo + "上门进行审核，请您配合工作人员的审核工作。");
                        } else if (resType == 2 || resType == 3 || resType == 5 || resType == 6) {
                            sendMsgContent = new StringBuilder(dwellerName + "您好，您在" + depName + "窗口办理的" + bizzName + "业务进度更新：" + bizzTypeInfo + "。");
                        }

                        if (resType == 8) { // 上门审核失败之后
                            if (StringUtils.isNoneBlank(StringUtils.trim(resNote))) { // 10个字以内
                                sendMsgContent.append("备注：" + resNote);
                                bizzTypeInfo = bizzTypeInfo + "，备注：" + resNote;
                            }
                        }

                        // 封装发送短信相关参数
                        Map<String, Object> msgMap = new HashMap<>();
                        msgMap.put("dweTelephone", dwellerPhone);
                        msgMap.put("dweName", dwellerName);
                        msgMap.put("depName", depName);
                        msgMap.put("bizzName", bizzName);
                        msgMap.put("res", bizzTypeInfo);
                        String msgParam = JSON.toJSONString(msgMap);

                        // 调用第三方发送短信接口发送信息
                        String msgResponse = null;
                        if (resType == 4) { // 上门审核通知
                            msgResponse = (String) HttpToSMS.sendSMSToHome(msgParam);
                        } else {
                            msgResponse = (String) HttpToSMS.sendSMS(msgParam);
                        }

                        if (msgResponse == null) {
                            logger.info("发送失败");
                            map = ResultMap.creatReault("11", "失败", "短信发送失败");
                            logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                        }
                        if (msgResponse != null) {
                            JSONObject msgJsonObject = JSON.parseObject(msgResponse);
                            if (StringUtils.equals(msgJsonObject.getString("status"), "-1")) { // 发送失败
                                logger.info("发送失败");
                                map = ResultMap.creatReault("11", "失败", "短信发送失败");
                                logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                            }

                            if (StringUtils.equals(msgJsonObject.getString("status"), "99")) { // 发送失败
                                logger.info("发送失败");
                                map = ResultMap.creatReault("11", "失败", "短信发送失败");
                                logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                            }

                            if (StringUtils.equals(msgJsonObject.getString("status"), "0")) { // 发送成功
                                logger.info("发送成功");
                                map = ResultMap.creatReault("1", "成功", "短信发送成功");

                                // 将发送的信息存入数据库
                                // 1.更新dweller_message表数据
                                String msgId = UUID.randomUUID().toString().replace("-", ""); // 消息唯一标识
                                Integer msgType = 0; // 0:短信 1:微信 2:站内
                                String sqlInsert = "INSERT INTO \n" +
                                        "\tmessage(\n" +
                                        "\t\tID,\n" +
                                        "\t\tdep_id,\n" +
                                        "\t\tbizz_id,\n" +
                                        "\t\tdweller_id,\n" +
                                        "\t\tofficer_id,\n" +
                                        "\t\tcontent,\n" +
                                        "\t\tTYPE,\n" +
                                        "\t\tcreate_time,\n" +
                                        "\t\tupdate_time,\n" +
                                        "\t\tstamp,\n" +
                                        "\t\tversion,\n" +
                                        "\t\tvisit_plat,\n" +
                                        "\t\tvisit_type,\n" +
                                        "\t\tis_used\n" +
                                        "\t)\n" +
                                        "VALUES\n" +
                                        "\t\t(\n" +
                                        "\t\t'" + msgId + "',\n" +
                                        "\t\t'" + depId + "',\n" +
                                        "\t\t'" + bizzId + "',\n" +
                                        "\t\t'" + dwellerId + "',\n" +
                                        "\t\t'" + offId + "',\n" +
                                        "\t\t'" + sendMsgContent + "',\n" +
                                        "\t\t" + msgType + ",\n" +
                                        "\t\t'" + createTime + "',\n" +
                                        "\t\t'" + updateTime + "',\n" +
                                        "\t\t'" + stamp + "',\n" +
                                        "\t\t" + version + ",\n" +
                                        "\t\t" + visitPlat + ",\n" +
                                        "\t\t" + visitType + ",\n" +
                                        "\t\t" + isUsed + "\n" +
                                        "\t)";
                                // 调用数据库
                                int update = baseDao.update(sqlInsert);
                                if (update <= 0) {
                                    logger.info("插入message消息表失败");
                                    map = ResultMap.creatReault("77", "失败", "数据库操作失败");
                                    logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                                    return map;
                                }

                                // 2.更新message表数据
                                String dwellerMsgId = UUID.randomUUID().toString().replace("-", ""); // 消息唯一标识

                                // 封装sql
                                sqlInsert = "INSERT INTO \n" +
                                        "\tdweller_message(\n" +
                                        "\t\tID, \n" +
                                        "\t\tmessage_id,\n" +
                                        "\t\tdweller_id,\n" +
                                        "\t\tTYPE,\n" +
                                        "\t\tread_type,\n" +
                                        "\t\tcreate_time,\n" +
                                        "\t\tupdate_time,\n" +
                                        "\t\tstamp,\n" +
                                        "\t\tversion,\n" +
                                        "\t\tvisit_plat,\n" +
                                        "\t\tvisit_type,\n" +
                                        "\t\tis_used\n" +
                                        "\t)\n" +
                                        "VALUES\n" +
                                        "\t\t(\n" +
                                        "\t\t'" + dwellerMsgId + "',\n" +
                                        "\t\t'" + msgId + "',\n" +
                                        "\t\t'" + dwellerId + "',\n" +
                                        "\t\t" + msgType + ",\n" +
                                        "\t\t" + 1 + ",\n" +
                                        "\t\t'" + createTime + "',\n" +
                                        "\t\t'" + updateTime + "',\n" +
                                        "\t\t'" + stamp + "',\n" +
                                        "\t\t" + version + ",\n" +
                                        "\t\t" + visitPlat + ",\n" +
                                        "\t\t" + visitType + ",\n" +
                                        "\t\t" + isUsed + "\n" +
                                        "\t)";
                                // 调用数据源
                                int resUpdate = baseDao.update(sqlInsert);
                                if (resUpdate <= 0) {
                                    logger.info("插入dweller_message居民消息关联表失败");
                                    map = ResultMap.creatReault("77", "失败", "数据库操作失败");
                                    logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
                                    return map;
                                }

                                logger.info("保存数据成功");
                                map = ResultMap.creatReault("0", "成功", "保存数据成功");
                            }
                        }
                    }
                }
            } else {
                logger.info("参数为空");
                map = ResultMap.creatReault("88", "失败", "参数为空");
            }
        } catch (ClassCastException e) {
            logger.info("后台处理异常，异常信息1：" + ExceptionDetailMessage.getExceptionDetail(e));
            map = ResultMap.creatReault("98", "失败", "参数类型错误");
            logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
            return map;
        } catch (Exception e) {
            logger.info("后台处理异常，异常信息1：" + ExceptionDetailMessage.getExceptionDetail(e));
            map = ResultMap.creatReault("99", "失败", "后台处理异常");
            return map;
        }

        // 4.返回发送成功或者失败
        logger.info("出去《推送消息 - 短信 - 居住证办理》sendResidencePermit()方法；出参 = " + JSON.toJSONString(map));
        return map;
    }
}
